﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;
using LdExpress;
using System.Text.RegularExpressions;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    public string ContainerPage = "./Goods.aspx";
    public string strCondition = "";
    public int NumOfItemsPerPage = 100;
    public string strPager = "";
    public string strIDs = "";
    public string strError = "";
    public string strMess = "";
    public int nPermissions = 0;
    public int nPage = 1;
    public string strPageID = "page_number";
    public string strLetter = "letter";
    public string strAlphaBet = "";
    string strChar = "";
    string strExcluded = "PortsID";
    public string strPartQuery = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Goods"] != null && int.Parse(Session["Goods"].ToString()) > 0)
        {
            if (Session["Goods"] != "") nPermissions = int.Parse(Session["Goods"].ToString());
        }
        if (nPermissions < 1)
            Response.Redirect("Login.aspx");
        try
        {

            if (Request.QueryString[strLetter] != null)
            {
                strChar = Request.QueryString[strLetter].ToString();
                strCondition = " AND Name like '" + strChar + "%'";
            }
        }
        catch
        {
            strChar = "";
        }

        try
        {

            if (Request.QueryString[strPageID] != null)
                nPage = int.Parse(Request.QueryString[strPageID]);
        }
        catch
        {
            nPage = 1;
        }
        if (nPage <= 0) nPage = 1;

        if (!Page.IsPostBack)
        {
            LoadDropDownGoodsCood();
            if (Request.QueryString["action"] == "delete")
            {
                Goods obj = new Goods();
                obj.ID = long.Parse(Request.QueryString["ID"]);
                obj.Load(GV.cn, GV.trans);
                obj.Delete(GV.cn, GV.trans);
                Response.Redirect("Goods.aspx");
            }
            if (nPermissions < 3) btnSave.Visible = false;
            #region Build List
            int nNumOfPages = 0;

            DataTable dtList;
            BuildList(NumOfItemsPerPage, nPage, out nNumOfPages, out dtList, null, null);
            if (nPage > nNumOfPages) nPage = 1;

            rptData.DataSource = dtList;
            rptData.DataBind();
            #endregion
            #region Build Pager
            CommonFunctions.BuildPager(Request, nNumOfPages, nPage, strPageID, ContainerPage, out strPager);
            #endregion
            CommonFunctions.BuildAphabetSearch(Request, ContainerPage, strLetter, strChar, strExcluded, out strAlphaBet);
        }

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        strError = "";
        strMess = "";
        Goods obj = null;
        GV.ErrorMessage = "";
        foreach (RepeaterItem item in rptData.Items)
        {
            obj = new Goods();
            string strID = ((HiddenField)item.FindControl("txtID")).Value.Trim();
            string strMa = ((TextBox)item.FindControl("txtMa")).Text.Trim();
            string strTen = ((TextBox)item.FindControl("txtTen")).Text.Trim();
            if (strID.Length > 0)//Update
            {
                bool isPlanSave = false;

                obj.ID = long.Parse(strID);
                obj.Load(GV.cn, GV.trans);
                if (obj.Code != strMa)
                {
                    obj.Code = strMa;
                    isPlanSave = true;
                }
                if (obj.Name != strTen)
                {
                    obj.Name = strTen;
                    isPlanSave = true;
                }
                if (isPlanSave)
                {
                    obj.Save(GV.cn, GV.trans);
                }

            }
            if (strTen.Length > 0 && strID.Length == 0)
            {
                obj.ID = -1;
                obj.Code = strMa;
                obj.Name = strTen;
                obj.Save(GV.cn, GV.trans);
                if (GV.ErrorMessage.Length > 0)
                {
                    lblError.Text = GV.ErrorMessage;
                }
                else
                    Response.Redirect("Goods.aspx");
            }
        }

    }

    private void BuildList(int NumOfItemsPerPage, int PageNo, out int nNumOfPages, out DataTable dtList, MySqlConnection cn, MySqlTransaction trans)
    {
        DataSet ds = null;
        dtList = new DataTable();
        dtList.Columns.Add("Index");
        dtList.Columns.Add("ID");
        dtList.Columns.Add("Ma");
        dtList.Columns.Add("Ten");
        dtList.Columns.Add("Action");
        //Get List by SoIDs
        nNumOfPages = 0;

        strCondition = "";
        bool isDisplay = false;
        if (Session["changeGoodsSourceID"] != null && Session["changeGoodsSourceID"].ToString() != "")
        {
            strCondition = " AND ID =" + Session["changeGoodsSourceID"].ToString();
            isDisplay = true;
        }
        if (nPermissions >= 9)
            isDisplay = true;
        if (!isDisplay) return;
        Goods.GetList(strCondition, "numCode", NumOfItemsPerPage, PageNo, out nNumOfPages, out ds, GV.cn, GV.trans);

        string strAllQueryString = "";
        string[] strValues = new string[] { "", "", "", "", "" };
        dtList.Rows.Add(strValues);
        foreach (string strKey in Request.QueryString.AllKeys)
        {
            if (strKey != null)
            {
                if ((strKey.ToLower() != "action") && (strKey.ToLower() != "id"))
                    strAllQueryString = strAllQueryString + "&" + strKey + "=" + Request.QueryString[strKey].ToString();
            }
        }

        if (strAllQueryString.Length >= 1)
            strAllQueryString = strAllQueryString.Substring(1);

        if (ds != null)
        {
            int count = 0;
            if (Request.QueryString["page_number"] != null && Request.QueryString["page_number"].ToString() != "")
            {
                int pagenumber = 1;
                try
                {
                    pagenumber = int.Parse(Request.QueryString["page_number"]);
                    count = NumOfItemsPerPage * (pagenumber - 1);
                }
                catch { count = 0; }
            }
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                count++;
                //string strSTT = dr["Sort"].ToString();
                string strIndex = count.ToString();
                string ID = dr["ID"].ToString();
                string Ma = dr["Code"].ToString();
                string Ten = dr["Name"].ToString();
                string strAction = "";
                if (nPermissions > 7)
                {
                    if (strAction.Length > 0) strAction += " - ";
                    strAction += "<a href=\"javascript:doDelete('" + System.Web.HttpUtility.HtmlEncode(dr["ID"].ToString()) + "')\">DEL</a>";
                }
                strValues = new string[] { strIndex, ID, Ma, Ten, strAction };
                dtList.Rows.Add(strValues);
            }
        }
    }


    protected void ddCode_Selected(object sender, EventArgs e)
    {
        Session["changeGoodsSourceID"] = ddCode.SelectedValue.ToString();
        Response.Redirect("Goods.aspx");
    }
    protected void ddName_Selected(object sender, EventArgs e)
    {
        Session["changeGoodsSourceID"] = ddName.SelectedValue.ToString();
        Response.Redirect("Goods.aspx");
    }
    private void LoadDropDownGoodsCood()
    {
        string sqlCommand = "Select ID,Code From tbl_Goods;Select ID,Name From tbl_Goods order by Name";
        DataSet ds = DBModule.ExecuteQuery(sqlCommand, GV.cn, GV.trans);
        DataTable dtb = ds.Tables[0];
        ddCode.DataSource = dtb;
        ddCode.DataValueField = dtb.Columns[0].ToString();
        ddCode.DataTextField = dtb.Columns[1].ToString();
        ddCode.DataBind();
        ddCode.Items.Insert(0, new ListItem("- Code -", ""));
        if (Session["changeGoodsSourceID"] != null && ds.Tables[0].Rows.Count > 0)
        {
            try
            {
                ddCode.SelectedValue = Session["changeGoodsSourceID"].ToString();
            }
            catch { }
        }
        //Name
        dtb = ds.Tables[1];
        ddName.DataSource = dtb;
        ddName.DataValueField = dtb.Columns[0].ToString();
        ddName.DataTextField = dtb.Columns[1].ToString();
        ddName.DataBind();
        ddName.Items.Insert(0, new ListItem("- Name -", ""));
        if (Session["changeGoodsSourceID"] != null && ds.Tables[0].Rows.Count > 0)
        {
            try
            {
                ddName.SelectedValue = Session["changeGoodsSourceID"].ToString();
            }
            catch { }
        }
    }
    protected void txtKeyword_TextChanged(object sender, EventArgs e)
    {
        if (txtKeyword.Text.Length > 0)
        {
            Goods obj = new Goods();
            obj.LoadByCode(hdnValue.Value, GV.cn, GV.trans);
            if (obj.ID > 0)
            {
                Session["changeGoodsSourceID"] = obj.ID.ToString();
                Response.Redirect("Goods.aspx");
            }
        }
    }
}
